local skel = fk.CreateSkill {
  name = "emo__yitong",
}

Fk:loadTranslationTable{
  ["emo__yitong"] = "异瞳",
  [":emo__yitong"] = "游戏开始时，你将牌堆中随机两张颜色不同的非伤害普通锦囊置于你的武将牌上，称为“目”。每回合各限一次，你可以将一张颜色与一张“目”牌相同的手牌当另一张“目”牌使用。",

  ["#emo__yitong"] = "异瞳：你可如此转化手牌：%arg",
  ["emo__yitongEye"] = "目",
}

--- 获得此颜色对应的牌名
local function yitongColor2Name (player, color)
  if table.contains(player:getTableMark("emo__yitong_used-turn"), color) then return nil end
  for _, id in ipairs(player:getPile("emo__yitongEye")) do
    if Fk:getCardById(id):getColorString() ~= color then
      return Fk:getCardById(id).name
    end
  end
end

skel:addEffect("viewas", {
  derived_piles = "emo__yitongEye",
  pattern = ".|.|.|.|.|trick",
  handly_pile = true,
  prompt = function (self, player, selected_cards, selected)
    local list = {}
    for _, color in ipairs({"red", "black"}) do
      local name = yitongColor2Name(Self, color)
      if name then
        table.insert(list, Fk:translate(color).."：["..Fk:translate(name).."]")
      end
    end
    return "#emo__yitong:::"..table.concat(list, ", ")
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and table.contains(player:getHandlyIds(true), to_select)
    and Fk:getCardById(to_select).color ~= Card.NoColor
    and not table.contains(player:getTableMark("emo__yitong_used-turn"), Fk:getCardById(to_select):getColorString())
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return nil end
    local name = yitongColor2Name(Self, Fk:getCardById(cards[1]):getColorString())
    if not name then return nil end
    local c = Fk:cloneCard(name)
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
  before_use = function(self, player, use)
    player.room:addTableMark(player, "emo__yitong_used-turn", use.card:getColorString())
  end,
  enabled_at_play = function(self, player)
    return #player:getTableMark("emo__yitong_used-turn") < 2 and #player:getPile("emo__yitongEye") > 0
  end,
  enabled_at_response = function (self, player, response)
    if #player:getTableMark("emo__yitong_used-turn") < 2 and #player:getPile("emo__yitongEye") > 0 and
    not response and Fk.currentResponsePattern and #player:getHandlyIds() > 0 then
      local names = {yitongColor2Name(player, "red"), yitongColor2Name(player, "black")}
      return table.find(names, function (name)
        return name and Exppattern:Parse(Fk.currentResponsePattern):matchExp(name)
      end)
    end
  end,
})

skel:addEffect(fk.GameStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local red, black = {}, {}
    for _, id in ipairs(room.draw_pile) do
      local c = Fk:getCardById(id)
      if c:isCommonTrick() and not c.is_damage_card then
        if c.color == Card.Red then
          table.insert(red, id)
        elseif c.color == Card.Black then
          table.insert(black, id)
        end
      end
    end
    local cards = {}
    if #red > 0 then table.insert(cards, table.random(red)) end
    if #black > 0 then table.insert(cards, table.random(black)) end
    if #cards == 0 then return end
    player:addToPile("emo__yitongEye", cards, true, skel.name)
  end,
})

return skel
